package activiti3;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.List;

/**
 * 各种查询
 *
 *
 */
public class ChaXun {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    /**
     * 根据执行人查询正在执行的任务
     */
    public void testQueryTaskByAssignee(){
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .orderByTaskCreateTime()
                .desc()
                .taskAssignee("审批人")
                .list();
        //遍历
        if(null != list && list.size()>0){
            for(Task task : list){
                System.out.println(task.getId());
            }
        }
    }
    /**
     * 查询所有正在执行的任务
     */
    public void testQueryTask(){
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .list();
    }
    /**
     * 根据piid查询任务
     * piid 流程实例ID
     */
    public void testQueryTaskByPiId(){
        String piId = "";
        processEngine.getTaskService().createTaskQuery()
                .executionId(piId)
                .list();
    }

    /**
     * 根据piid得到当前正在执行的的流程实例的正在活动的节点
     */
    public void testActivity(){
        String piid = "";
        ProcessInstance pi = processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(piid)
                .singleResult();
        String activityId = pi.getActivityId();
        System.out.println(activityId);
    }
    /**
     * 查询一个流程实例所有节点
     */
    public void testProcessDefintionEntry(){
        String pdid = "qingjia:1:801";//流程id
        /**
         * 根据piid得到ProcessDefintion
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processEngine.getRepositoryService()
                .getProcessDefinition(pdid);
        /**
         * ActivityImpl是一个对象
         * 一个ActivityImpl代表processDefinitionEntity的一个节点
         */
        List<ActivityImpl> activities = processDefinitionEntity.getActivities();
        if(null != activities && activities.size()> 0){
            for(ActivityImpl activity : activities){
                System.out.println(activity.getId());
            }
        }
    }
    /**
     * 查询一个流程箭头PvmTransition
     */
    public void testSequrnceFlow(){
        String pdid = "qingjia:1:801";//流程id
        /**
         * 根据pdid得到ProcessDefintion
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processEngine.getRepositoryService()
                .getProcessDefinition(pdid);
        /**
         * ActivityImpl是一个对象
         * 一个ActivityImpl代表processDefinitionEntity的一个节点
         */
        List<ActivityImpl> activities = processDefinitionEntity.getActivities();
        if(null != activities && activities.size()> 0){
            for(ActivityImpl activity : activities){
                /**
                 * 得到一个activityimpl的所有outgoing
                 */
                List<PvmTransition> outgoingTransitions = activity.getOutgoingTransitions();
                for (PvmTransition pvmTransition : outgoingTransitions) {
                    System.out.println(pvmTransition.getId());
                }
            }
        }
    }



    /**
     * 查询历史任务
     */
    public void testQueryHistoryTask(){
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService
                .createHistoricTaskInstanceQuery()
                .taskDeleteReason("complete")//该条件必须加，因为正在执行的实例也在历史表
                .list();
    }

    /**
     * 得到当前长在执行的流程实例的activityimpl-->PvmTransition
     */
    public void testQueryActivityImpl_ing(){
        String pdid = "qingjia:1:801";//流程id
        String piid = "801";
        /**
         * 根据pdid得到ProcessDefintion
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) processEngine.getRepositoryService()
                .getProcessDefinition(pdid);
        /**
         * 根据piid取到activityId
         */
        ProcessInstance pi = processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(piid)
                .singleResult();
        ActivityImpl activity = processDefinitionEntity.findActivity(pi.getActivityId());
        System.out.println("流程实例ID" + pi.getId());
        System.out.println("当前正在执行的节点" + activity.getId());

    }











}
